package com.lazason.wib4android;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import weibo4andriod.Comment;
import weibo4andriod.DirectMessage;
import weibo4andriod.Paging;
import weibo4andriod.Weibo;
import weibo4andriod.WeiboException;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;

import com.lazason.wib4android.AsyncImageLoader.ImageCallback;

public class MessageActivity extends BasicWib {
    
	 private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
	 private static final int REQ_CODE_PICTURES = 1000;
	 private static final int REQ_CODE_CAMERA = 1001;
	 private String photoTemp;
	    private ArrayList<String> matches;
	    private EditText editText;
	    private EditText sendto;
	    TextView sucess ;
	    private Button post;  
	    private String cachePaht; 
	    private ProgressDialog m_ProgressDialog = null; 
	    private Runnable postThread;  
	    private String weiboId;
	    private String username;
	    private String profile;
	    private String createdAt;
	    String picture;
	    String weiboText;
	    Thread thread;
	   
	    AsyncImageLoader asyncImageLoader;
	    /**
	     * Called with the activity is first created.
	     */
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        super.initActivity(savedInstanceState,R.layout.messages);
	        MyApplication.getInstance().addActivity(this);
	        // Inflate our UI from its XML layout description.
	        cachePaht = this.getExternalCacheDir().toString();
	        // Get display items for later interaction  
	        
	        asyncImageLoader = new AsyncImageLoader();
			
	     
	        editText = (EditText) findViewById(R.id.content); 
	        sendto = (EditText) findViewById(R.id.sendto); 
	        post =  (Button) findViewById(R.id.coment_submit);
	        postThread = new Runnable(){  
	            @Override  
	            public void run() {  
	                postWeibo();  
	            }  
	        };  
	       
	        post.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					 
						 thread =  new Thread(null, postThread,"postWeiboThread"); 
						 thread.start();
						 m_ProgressDialog = ProgressDialog.show(MessageActivity.this,      
					              "Please wait...", "Uploading data ...", true);  
						
					
				}
				
			});
	        
	       
	        PackageManager pm = getPackageManager();
	         
	        listView = (ListView) findViewById(android.R.id.list);
	        adapter = new MyAdapter(this, data,
					R.layout.comments_item, new String[] { "sender_profile", "sender_name","message",
							"created_at"}, new int[] {
							R.id.comment_profile,R.id.comment_username, R.id.comment, R.id.comment_createdTime,}, listView);

	        TimeLineTask commentsTask = new TimeLineTask();
	        commentsTask.execute(page);
	        listView.setOnScrollListener(new OnScrollListener() {

				@Override
				public void onScroll(AbsListView view, int firstVisibleItem,
						int visibleItemCount, int totalItemCount) {
				
				}

				@Override
				public void onScrollStateChanged(AbsListView paramAbsListView,
						int paramInt) {
					// TODO Auto-generated method stub

				}

			});
	    }

	  

	 
	
	    @Override
		protected void onDestroy() {
			// TODO Auto-generated method stub
			super.onDestroy();
			MyApplication.getInstance().removeActivity(this);
			
		}
	    

	
	    private void postWeibo(){  
	    	Weibo weibo=OAuthConstant.getInstance().getWeibo();
			String text = "";
			try {
				DirectMessage directMessage = weibo.sendDirectMessage(sendto.getText().toString(),editText.getText().toString());
			} catch (WeiboException e) {
				// TODO Auto-generated catch block
				 
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				 
			} finally{
				 
				Intent intent = new Intent();
				intent.setClass(this, MessageActivity.class);
				startActivity(intent);
			}
	    }
	    
@Override
protected void onResume() {
	// TODO Auto-generated method stub
	if (data != null) {
		  adapter = new MyAdapter(this, data,
					R.layout.comments_item, new String[] { "sender_profile", "sender_name","message",
							"created_at"}, new int[] {
							R.id.comment_profile,R.id.comment_username, R.id.comment, R.id.comment_createdTime,}, listView);

		int lastItem = listView.getFirstVisiblePosition();

		listView.setAdapter(adapter);
		listView.setSelection(lastItem);
	}
//	if(window==null){
//		Timer timer = new Timer();  
//        timer.schedule(new initPopupWindow(), 100);  
//		}
	super.onResume();
}

@Override
protected void addData(List list) {
	
	for (Object message : list) {
		if(message instanceof weibo4andriod.DirectMessage){
			Map<String, Object> map = Util.message2Map((DirectMessage)message);

		MyApplication.data.add(map);
		}

	}
}

@Override
protected List readTimeline(int arg) throws WeiboException {
	// TODO Auto-generated method stub
	 

	 

	return weibo.getDirectMessages(new Paging(arg) );
}





@Override
void refresh() {
	// TODO Auto-generated method stub
	
}
}